﻿using Adp;
using Adp.Common;
using Adp.Core.Attribute;
using Microsoft.AspNetCore.Mvc;
using Yqd.Core;
using Yqd.Enm;
using Yqd.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Yqd.Controllers.Filter;
using System.Threading;

namespace Yqd.Controllers
{
    public class QuotaFieldController : BaseController
    {
        YqdContext ctx;

        public QuotaFieldController(YqdContext ctx)
        {
            this.ctx = ctx;
        }
        public ActionResult GetList()
        {
            var start = Form.GetInt("start");
            var limit = Form.GetInt("limit");
            var key = Form.GetString("key");

            var where = " 1=1 ";
            if (key.Trim() != "")
            {
                where += " and lower(ltrim(rtrim([FieldName]))) like {0}";
            }
          
            //if (unbind)
            //{
            //    where += " and (OrganizationCode is null or OrganizationCode='')";
            //}
            var (totalcount, data) = ctx.Database.SqlPager<Yqd_QuotaField>(start, limit, "*", where, "Tactic Desc", "%" + key.ToLower().Trim() + "%");
          
            return this.ToJson(new
            {
                success = true,
                totalcount,
                data = data.Select(q => new
                {
                    q.FieldID,
                    q.FieldName,
                    q.Tactic
                })
            });

        }
        //[Master]
        public ActionResult Add(Yqd_QuotaField model)
        {

            if (!ModelState.IsValid)
            {
                return this.ToJson(new
                {
                    success = false,
                    stateMsg = from sk in ModelState.AsEnumerable()
                               where sk.Value.Errors.Count() > 0
                               select new { id = sk.Key, msg = sk.Value.Errors.FirstOrDefault().ErrorMessage }
                });
            }
            var QF = ctx.Yqd_QuotaField.FirstOrDefault(it=>it.FieldName==model.FieldName);
            if (QF != null)
            {
                return this.ToJson(new
                {
                    success = false,
                    msg="该字段已存在，请勿重复添加"
                });
            }
            ctx.Yqd_QuotaField.Add(model);
            var ret = ctx.SaveChanges();
            return this.ToJson(new
            {
                success = true
            });
        }

        public ActionResult UpdateTop()
        {
            var FieldID = Form.GetInt("FieldID");
            var Tactic = Form.GetInt("Tactic");
            if (FieldID > 0)
            {
                var ret = ctx.Database.ExecuteSql("update Yqd_QuotaField set Tactic={1} where FieldID ={0}", FieldID, Tactic);
                return this.ToJson(new
                {
                    success = true,
                    ret
                });
            }
            return this.ToJson(new
            {
                success = false,
                msg = "字段不存在"
            });
        }


    }
}
